var express = require("express");
var router = express.Router();
const db = require("../model/db");
const { log } = require("debug/src/browser");

/* GET users listing. */
router.get("/index", function (req, res, next) {
  let name = req.query.name;

  if (name) {
    let sql = `SELECT vehicle.veid,vehicle.image,vehicle.vname,vehicle.platenumber,store.name,vehicle.vstatus,vehicle.annualInsuranceDate FROM vehicle,raise,store WHERE vehicle.rid=raise.id and vehicle.sid = store.id and vehicle.vname like '%${name}%'`;
    db.query(sql, function (err, result) {
      if (err) {
        console.log(err, "获取数据出错了");
      } else {
        result.forEach((item) => {
          item["key"] = item.veid;
        });
        res.send({ code: 200, msg: "获取成功", data: result });
      }
    });
  } else {
    let sql = `SELECT vehicle.veid,vehicle.image,vehicle.vname,vehicle.platenumber,store.name,vehicle.vstatus,vehicle.annualInsuranceDate FROM vehicle,raise,store WHERE vehicle.rid=raise.id and vehicle.sid = store.id`;
    db.query(sql, function (err, result) {
      if (err) {
        console.log(err, "获取数据出错了");
      } else {
        // 给每个节点添加一个唯一key
        result.forEach((item) => {
          item["key"] = item.id;
        });
        res.send({ code: 200, msg: "获取成功", data: result });
      }
    });
  }
});
router.post("/update", function (req, res, next) {
  console.log(req.body);
  let sql = `UPDATE vehicle SET vname='${req.body.vname}',platenumber='${req.body.platenumber}',vstatus='${req.body.vstatus}',annualInsuranceDate='${req.body.annualInsuranceDate}' WHERE veid='${req.body.veid}'`;
  db.query(sql, function (err, result) {
    if (err) {
      console.log(err, "获取数据出错了");
    } else {
      res.send({ code: 200, msg: "修改成功", data: result });
    }
  });
});
module.exports = router;
